package com.sphd.Restaurant.presenter;

import android.widget.Toast;

import com.android.volley.VolleyError;
import com.sphd.Restaurant.infos.TopupInfo;
import com.sphd.Restaurant.interfFace.VolleyCallBack;
import com.sphd.Restaurant.utils.JsonUtil;
import com.sphd.Restaurant.utils.VolleyErrorHelper;
import com.sphd.Restaurant.views.RechargeCardView;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Hu on 2016/10/13.
 */

public class RechargeCardP extends BasePresenter implements VolleyCallBack<JSONObject> {
    RechargeCardView cardView;

    public RechargeCardP(RechargeCardView cardView) {
        this.cardView = cardView;
    }

    @Override
    public void addData(String url) {
        cardView.showdialog();
        sendJsonObjectUrl(url, this);
    }

    @Override
    public void onResponse(JSONObject object) {
        try {
            List<TopupInfo> topupInfos = JsonUtil.getListFromString(object.getString("res"),TopupInfo.class);
           cardView.sendList(topupInfos);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        cardView.dissmissdialog();
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(context, VolleyErrorHelper.getMessage(error),Toast.LENGTH_SHORT).show();
            cardView.dissmissdialog();
    }
}
